from django.db import models

# Create your models here.
from rest_framework import serializers


class  Provincial(models.Model):
   # """
   #  1、null = True       数据库中字段是否可以为空
   #  2、blank = True      django的Admin中添加数据时是否可允许空值
   #  3、verbose_name　　      Admin中字段的显示名称
   #  4、unique=True　　不允许重复
   # 5、django默认情况下每一个主表的对象都有一个是外键的属性，
   # 可以通过它查询到所有关于子表的信息，这个属性的名字就是子表的名称小写加上_set，具体到这个就是student_set，
   # 6、也可以用related_name="children"来表示
   #  """

    Pr_name = models.CharField(max_length=255,unique=True, verbose_name='地区')
    parent = models.ForeignKey('self', blank=True, null=True, on_delete=models.CASCADE, related_name="children",verbose_name='上级部门')
    user = models.ForeignKey('auth.user',blank=True, null=True, on_delete=models.CASCADE, related_name="job",verbose_name='管理人员编号')
    class Meta:
        db_table = 'v1_provincial'

    def __str__(self):
        return self.Pr_name




class AreaManage(models.Model):
    Pr_name = models.CharField(max_length=255,unique=True, verbose_name='地区')
    parent = models.ForeignKey('self', blank=True, null=True, on_delete=models.CASCADE, related_name="children",verbose_name='上级部门')
    user = models.ManyToManyField('auth.user',verbose_name='操作人员')

    class Meta:
        db_table = 'AreaManage'

    def __str__(self):
        return self.Pr_name


def get_image_path(instance, filename):
    print(instance.area_id)
    return 'image/%s/%s' % (instance.area_id, filename)

class Image(models.Model):
    """调度照片"""
    area_id = models.ForeignKey('AreaManage',to_field='id',on_delete=models.CASCADE, verbose_name='地区',default='')
    image_url = models.ImageField(upload_to=get_image_path, verbose_name='图片路由')

    class Meta:
        db_table = 'pr_image'



